<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace common\models\system;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "sys_config".
 *
 * @property int $id 参数ID
 * @property string $name 参数名称
 * @property string $config_key 参数标识
 * @property string $type 参数类型
 * @property string $groups 参数所属分组
 * @property string|null $is_sys 是否系统内置 0否 1是
 * @property string|null $value 参数值
 * @property int|null $list_sort 排序
 * @property string|null $remark 参数说明
 * @property int|null $create_time 创建时间
 * @property int|null $update_time 更新时间
 */
class Config extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName(): string
    {
        return 'sys_config';
    }

    /**
     * {@inheritdoc}
     */
    public function rules(): array
    {
        return [
            [['name', 'config_key', 'type'], 'required'],

            [['config_key', 'remark', 'value', 'groups'], 'default', 'value' => ''],
            ['list_sort', 'default', 'value' => 10],
            ['is_sys', 'default', 'value' => '0'],

            [['name', 'config_key', 'type', 'value', 'remark'], 'trim'],

            ['config_key', 'match', 'pattern' => '/^[A-Za-z0-9._-]+$/', 'message' => '{attribute}必须是字母、数字、下划线、英文点或破折号'],

            [['name', 'config_key', 'type', 'groups'], 'string', 'max' => 64],
            ['remark', 'string', 'max' => 255],

            ['config_key', 'unique'],

            [['create_time', 'update_time'], 'safe'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'name' => '参数名称',
            'config_key' => '参数标识',
            'type' => '参数类型',
            'groups' => '参数分组',
            'is_sys' => '是否系统内置',
            'value' => '参数值',
            'list_sort' => '排序',
            'remark' => '参数说明',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
                ],
                'value' => function () {
                    return time();
                }
            ]
        ];
    }
}
